package com.zego.wawaji.ui.dialog.game;

import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.zego.wawaji.R;
import com.zego.wawaji.base.observer.MsgMgr;
import com.zego.wawaji.ui.dialog.popup.BottomPopup;
import com.zego.wawaji.wawaji_client.cmd.CommandUtil;
import com.zego.wawaji.wawaji_client.util.OnSoundClickListener;

/**
 * 开始游戏倒计时
 * Created by Su on 2017/12/8.
 */
public class StartGameDialog extends BottomPopup {
    private Handler mHandler = null;          // 倒计时
    private HandlerThread mMessageThread;
    private int countTimes = 0;   // 计时次数

    private int rspSeq;
    private String sessionData;

    private ImageView countImg;

    public StartGameDialog(FragmentActivity activity) {
        super(activity);
        setCancelableOutTouch(false);
    }

    public void setData(int rspSeq, String sessionData) {
        this.rspSeq = rspSeq;
        this.sessionData = sessionData;
    }

    @Override
    protected View makeContentView() {
        final View inflate = LayoutInflater.from(activity).inflate(R.layout.start_game_dialog, null);

        MsgMgr.getInstance().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mMessageThread = new HandlerThread("start-game-timer");
                mMessageThread.start();
                mHandler = new Handler(mMessageThread.getLooper(), new HandlerImpl());

                countImg = (ImageView) inflate.findViewById(R.id.count_img);
                inflate.findViewById(R.id.btn_close).setOnClickListener(new OnSoundClickListener() {
                    @Override
                    public void onSoundClick(View v) {
                        release();
                    }
                });
                sendMsg();
            }
        });
        return inflate;
    }

    private void sendMsg() {
        if(mHandler == null) return;
        Message message = new Message();
        message.what = HandlerImpl.MSG_COUNT_DOWN;
        mHandler.sendMessageDelayed(message, HandlerImpl.INTERVAL_COUNT_DOWN);
    }

    private void release() {
        CommandUtil.getInstance().confirmBoard(rspSeq, sessionData, 1, null);
        countTimes = 0;
        mHandler.removeMessages(HandlerImpl.MSG_COUNT_DOWN);
        mMessageThread.quit();
        mMessageThread = null;
        mHandler = null;
        dismiss();
    }

    /**
     * 计时器
     */
    private class HandlerImpl implements Handler.Callback {
        static final public int MSG_COUNT_DOWN = 0x1;                   // 倒计时
        static final public int INTERVAL_COUNT_DOWN = 1000;  // 倒计时间隔

        @Override
        public boolean handleMessage(Message message) {
            switch (message.what) {
                case MSG_COUNT_DOWN: {
                    MsgMgr.getInstance().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            countTimes++;

                            switch (countTimes) {
                                case 1:
                                    countImg.setImageResource(R.drawable.start_count02);
                                    sendMsg();
                                    break;

                                case 2:
                                    countImg.setImageResource(R.drawable.start_count03);
                                    sendMsg();
                                    break;

                                case 3:
                                    release();
                                    break;
                            }
                        }
                    });
                }
                break;
            }
            return true;
        }
    }
}
